#!/usr/bin/env python

import utils
from errors import *
from os3.types.date import Date


class Auth ( object ):
	def __init__ ( self, userman ):
		super ( Auth, self ).__init__()
		self._pwd_encr_type = userman._pwd_encr_type
		self._userman = userman


	def authenticate ( self, username, password ):
		user = self._userman.get ( username )
		if not user: return AUTH_BAD_USERNAME

		if not user["enabled"]: return AUTH_USER_DISABLED

		if user["exp_date"]:
			now = Date()
			if now > user["exp_date"]: return AUTH_PASSWORD_EXPIRED

		if utils.check_pwd ( password, user["password"], self._pwd_encr_type ):
			self._userman.modify ( username, { "last_log": 0 } )
			return AUTH_OK
		else:
			return AUTH_BAD_PASSWORD

